package com.yunquan.ohana.ui.calendar.lists.list

import android.os.Bundle
import androidx.activity.result.ActivityResult
import androidx.core.content.ContextCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.google.gson.Gson
import com.yunquan.ohana.BR
import com.yunquan.ohana.R
import com.yunquan.ohana.base.ui.BaseActivity
import com.yunquan.ohana.databinding.ActivityListsListBinding
import com.yunquan.ohana.entity.ListsEntity
import com.yunquan.ohana.ui.calendar.lists.list.adapter.ListsListAdapter
import java.util.Locale

class ListsListActivity : BaseActivity<ActivityListsListBinding, ListsListViewModel>() {
    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_lists_list
    }

    override fun initVariableId(): Int {
        return BR.listsListsViewModel
    }

    override fun initParam() {
        super.initParam()
        Locale.setDefault(Locale("en"))
        // 设置背景颜色
        window.statusBarColor = ContextCompat.getColor(this, R.color.white)
        // 设置文字颜色（true = 黑色文字，false = 白色文字）
        val controller = WindowInsetsControllerCompat(window, window.decorView)
        controller.isAppearanceLightStatusBars = true
    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        binding!!.adapter = ListsListAdapter()
        val bundle = intent.extras
        bundle?.let {
            viewModel!!.mDeviceId = it.getString("deviceId")
            viewModel!!.isUserType.set(it.getBoolean("userType", false))
            val dataJson = it.getString("data")
            if (dataJson != null) {
                viewModel!!.mData = Gson().fromJson(dataJson, ListsEntity::class.java)
                viewModel!!.title.set(viewModel!!.mData?.name)
            }
        }
        if (viewModel!!.title.get() == null) {
            viewModel!!.title.set(getString(R.string.lists))
        }
        viewModel!!.getListsItem()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        binding!!.smartRefreshLayout.setEnableRefresh(false)
        binding!!.smartRefreshLayout.setEnableLoadMore(false)
        binding!!.smartRefreshLayout.setOnRefreshListener {
            viewModel!!.getListsItem()
        }
        viewModel!!.showDelete.observe(this) {
            binding!!.tvDelete.setEnabled(it ?: false)
        }
        viewModel!!.ui.observe(this) {
            if (it == 0) {
                binding!!.tvSelectAll.post {
                    binding!!.tvSelectAll.helper.cornerRadius = resources.getDimensionPixelSize(R.dimen.dp_16).toFloat()
                }
            }
        }
    }

    override fun onActivityResult(result: ActivityResult) {
        super.onActivityResult(result)
        if (result.resultCode == 1005) {
            val bundle = result.data?.extras
            val dataJson = bundle?.getString("data")
            if (dataJson != null) {
                viewModel!!.mData = Gson().fromJson(dataJson, ListsEntity::class.java)
                viewModel!!.title.set(viewModel!!.mData?.name)
                viewModel!!.getListsItem()
            } else {
                setResult(1005)
                finish()
            }
        }
    }
}